from django import template
import os
from .. import inc 

register = template.Library()

class SelectRender:
  def __init__(self,name):
    self.name = name
    self.style = "" 
    self.fields = []
  def Render(self):
    rend_result = """\
                  <div class="extension-drop-list" style="%s">\
                    <input type="textfield" style="display:none" name="%s"></input>\
                    <div class='current'>Not Selected</div><ul>"""\
                     % (self.style,self.name)
    for field in self.fields:
      rend_result += "<li><h5>"+field+"</h5><span>&#9744</span></li>"
    tail = """</ul></div>"""
    return rend_result+tail
  def AddField(self,field_name):
    self.fields.append(field_name)
  def SetStyle(self,style):
    self.style = style

@register.filter("post")
def post(pname,sname):
  f = open(inc.CONFIG.SERVICES_PATH + sname+'/' + pname + '.post',"r")
  buf = f.read()
  f.close()
  return buf

@register.filter("template")
def template(cname):
  return "comp/_"+cname+".html"


@register.filter("select")
def select(selectname):
  return SelectRender(selectname)

@register.filter("style")
def style(ele_config,style):
  ele_config.SetStyle(style)
  return ele_config

@register.filter("addfield")
def addfield(ele_config,field):
  ele_config.AddField(field)
  return ele_config

@register.filter("render")
def render(ele_config):
  return ele_config.Render()
